
<?php

	include('Connect_DB.php');
	
	//Get array rows within donvi Table
	function View_Donvi(){
		$query = "SELECT * FROM donvi";
		$result = mysql_query($query) or die(mysql_error());
		return $result;		
	}
	
	//Get a row and fetch columns of row within array
	function View_Donvi_id($id){
		$query ="SELECT * FROM donvi WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
		return $row;
	}
	
	
	//Get array rows within nganh Table
	function View_Nganh(){
		$query = "SELECT * FROM nganh";
		$result = mysql_query($query) or die(mysql_error());
		return $result;	
	}
	
	//Get a row within nganh Table
	function View_Nganh_id($id){
		$query ="SELECT * FROM nganh WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
		return $row;
	}
	
	function View_Khoa(){
		$query = "SELECT * FROM khoadaotao";
		$result = mysql_query($query) or die(mysql_error());
		return $result;		
	}
	
	function View_Khoa_id($id){
		$query ="SELECT * FROM khoadaotao WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
		return $row;		
	}
	
	function Add_Donvi()
	{
		$id_unit = $_POST['id_unit'];
		$name_faculty = $_POST['name_faculty'];
		$name_subject = $_POST['name_subject'];
		 
		$query = "INSERT donvi SET maDonvi = '$id_unit', tenKhoa = '$name_faculty', tenBomon = '$name_subject'";
		$result = mysql_query($query);
	}
	
	//Uses to Check "Ma Don Vi" On the form with "maDonvi" is Primary key of "donvi" table within Database 
	function Error_Add_Donvi($id_unit)
	{
		$query = "SELECT *FROM donvi WHERE maDonvi ='$id_unit'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;
	}
	
	function Add_Nganh()
	{
		$ma_ng = $_POST['ma_ng'];
		$name = $_POST['name'];
		$full_name = $_POST['full_name'];
		$id_dv = $_POST['id_dv'];
		
		$query = "INSERT nganh SET maNganh = '$ma_ng', tenViettat = '$name', tenDaydu = '$full_name', maDonvi = '$id_dv'";
		$result = mysql_query($query);
	}
	
	function Error_Add_Nganh($ma_ng)
	{
		$query = "SELECT *FROM nganh WHERE maNganh ='$ma_ng'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;
	}
	
	function Add_Khoa()
	{
		$ma_kh = $_POST['ma_kh'];
		$ma_ng = $_POST['ma_ng'];
		$name_kh = $_POST['name_kh'];
		$nam = $_POST['nam'];
		
		$query = "INSERT khoadaotao SET maKhoaDT = '$ma_kh', maNganh = '$ma_ng', tenKhoaDT = '$name_kh', soNamDaotao = '$nam'";
		$result = mysql_query($query);
	}
	
	function Error_Add_Khoa($ma_kh)
	{
		$query = "SELECT *FROM khoadaotao WHERE maKhoaDT ='$ma_kh'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;
	}
	
	function Delete_Donvi($id)
	{
		$query = "DELETE FROM donvi WHERE id = $id";
		$result = mysql_query($query);
		
		if(!$result)
		{
			echo mysql_error();
		
		}
	}
	
	function Delete_Nganh($id)
	{
		$query = "DELETE FROM nganh WHERE id = $id";
		$result = mysql_query($query);
		
		if(!$result)
		{
			echo mysql_error();
		
		}
	}
	
	function Delete_Khoa($id)
	{
		$query = "DELETE FROM khoadaotao WHERE id = $id";
		$result = mysql_query($query);
		
		if(!$result)
		{
			echo mysql_error();
		
		}
	}
	
	function Edit_Donvi($id)
	{
		$id_unit = $_POST['id_unit'];
		$name_faculty = $_POST['name_faculty'];
		$name_subject = $_POST['name_subject'];
		//echo "Ma don vi la".$id_unit;
		$query = "UPDATE donvi SET maDonvi = '$id_unit', tenKhoa = '$name_faculty', tenBomon = '$name_subject' WHERE id ='$id'";
		$result = mysql_query($query);
	}
	
	function Error_Edit_Donvi($id, $id_unit){
		$query = "SELECT * FROM(SELECT *FROM donvi WHERE id!='$id') AS Temp_Table WHERE maDonvi = '$id_unit'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;	
	}
	
	function Edit_Nganh($id)
	{
		$ma_ng = $_POST['ma_ng'];
		$name = $_POST['name'];
		$full_name = $_POST['full_name'];
		$id_dv = $_POST['id_dv'];
		
		$query = "UPDATE nganh SET maNganh = '$ma_ng', tenViettat = '$name', tenDaydu = '$full_name', maDonvi = '$id_dv' WHERE id ='$id'";
		$result = mysql_query($query);	
	}
	
	function Error_Edit_Nganh($id, $ma_ng){
		$query = "SELECT * FROM(SELECT *FROM nganh WHERE id!='$id') AS Temp_Table WHERE maNganh = '$ma_ng'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;	
	}
	
	function Edit_Khoa($id)
	{
		$ma_kh = $_POST['ma_kh'];
		$ma_ng = $_POST['ma_ng'];
		$name_kh = $_POST['name_kh'];
		$nam = $_POST['nam'];
		
		$query = "UPDATE khoadaotao SET maKhoaDT = '$ma_kh', maNganh = '$ma_ng', tenKhoaDT = '$name_kh', soNamDaotao = '$nam' WHERE id ='$id'";
		$result = mysql_query($query);	
	}
	
	function Error_Edit_Khoa($id, $ma_kh){
		$query = "SELECT * FROM(SELECT *FROM khoadaotao WHERE id!='$id') AS Temp_Table WHERE maKhoaDT = '$ma_kh'";
		$result = mysql_query($query);
		$rows = mysql_num_rows($result);
		return $rows;	
	}
	
	function Count_Nganh(){
		$query="SELECT donvi.tenKhoa AS A, Count(tenDaydu) AS B
				FROM donvi LEFT JOIN nganh
				ON donvi.maDonvi = nganh.maDonvi
				GROUP BY tenKhoa ORDER BY Count(tenDaydu)";
		$result = mysql_query($query) or die(mysql_error());
		return $result;	
	}
		
?>	

